package com.xbts.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.constant.UserConstants;
import com.xbts.common.core.domain.model.LoginUser;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.CnCommonCloud;
import com.xbts.system.domain.CnCommonCloudType;
import com.xbts.system.domain.vo.AreaVo;
import com.xbts.system.mapper.CnCommonCloudMapper;
import com.xbts.system.service.ICnCommonCloudService;
import com.xbts.system.service.ICnCommonCloudTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 政务云Service业务层处理
 * 
 * @author xbts
 * @date 2023-08-23
 */
@Service
public class CnCommonCloudServiceImpl extends ServiceImpl<CnCommonCloudMapper, CnCommonCloud> implements ICnCommonCloudService {

    @Autowired
    private ICnCommonCloudTypeService cnCommonCloudTypeService;

    @Autowired
    private CnCommonCloudMapper commonCloudMapper;

    /**
     * 查询政务云列表
     *
     * @param cnCommonCloud 政务云
     * @return 政务云
     */
    @Override
    public List<CnCommonCloud> selectCnCommonCloudList(CnCommonCloud cnCommonCloud) {
        QueryWrapper<CnCommonCloud> queryWrapper = cnCommonCloud.queryWrapper();
        List<CnCommonCloud> list = list(queryWrapper);
        List<CnCommonCloudType> cloudTypeList = cnCommonCloudTypeService.list();
        Map<String, String> map = cloudTypeList.stream().collect(Collectors.toMap(CnCommonCloudType::getServerTypeId, CnCommonCloudType::getServerType));
        for (CnCommonCloud cloud : list) {
            cloud.setServerType(map.get(cloud.getServerTypeId()));
        }
        return list;
    }

    /**
     * 查询政务云
     *
     * @param govId 政务云主键
     * @return 政务云
     */
    @Override
    public CnCommonCloud selectCnCommonCloudByGovId(String govId) {
        return getById(govId);
    }

    /**
     * 新增政务云
     *
     * @param cnCommonCloud 政务云
     * @return 结果
     */
    @Override
    public boolean insertCnCommonCloud(CnCommonCloud cnCommonCloud) {
        if (cnCommonCloud.getTopped() && toopedNum()){
            throw new ServiceException("置顶数量不能超过4个，请先取消其他置顶");
        }
        if (StringUtils.isEmpty(cnCommonCloud.getServerTypeId())) {
            throw new ServiceException("政务云类型不能为空");
        }
        CnCommonCloudType cnCommonCloudType = cnCommonCloudTypeService.getById(cnCommonCloud.getServerTypeId());
        if (cnCommonCloudType == null) {
            throw new RuntimeException("政务云类型不存在");
        }
        cnCommonCloud.setServerType(cnCommonCloudType.getServerType());
        cnCommonCloud.setCreateBy(SecurityUtils.getUserId().toString());
        return save(cnCommonCloud);
    }

    /**
     * 修改政务云
     *
     * @param cnCommonCloud 政务云
     * @return 结果
     */
    @Override
    public boolean updateCnCommonCloud(CnCommonCloud cnCommonCloud) {
        CnCommonCloud dbCnCommonCloud = getById(cnCommonCloud);
        // 如果置顶状态发生改变，判断置顶数量是否超过最大数量
        if(dbCnCommonCloud.getTopped() != cnCommonCloud.getTopped() && cnCommonCloud.getTopped()){
            if (toopedNum()){
                throw new ServiceException("置顶数量不能超过4个，请先取消其他置顶");
            }
        }
        cnCommonCloud.setUpdateBy(SecurityUtils.getUserId().toString());
        cnCommonCloud.setUpdateTime(DateUtils.getNowDate());
        return updateById(cnCommonCloud);
    }

    /**
     * 批量删除政务云
     *
     * @param govIds 需要删除的政务云主键
     * @return 结果
     */
    @Override
    public boolean deleteCnCommonCloudByGovIds(List<String> govIds) {
        LambdaUpdateWrapper<CnCommonCloud> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(CnCommonCloud::getGovId, govIds);
        updateWrapper.set(CnCommonCloud::getDeleteFlag, "1");
        return update(updateWrapper);
    }

    /**
     * 删除政务云信息
     *
     * @param govId 政务云主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCnCommonCloudByGovId(String govId) {
      return removeById(govId);
    }

    @Override
    public List<CnCommonCloud> hostList() {
        LambdaQueryWrapper<CnCommonCloud> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CnCommonCloud::getTopped, "1");
        return list(queryWrapper);
    }

    @Override
    public List<AreaVo> areaList() {
        return commonCloudMapper.areaList();
    }

    /**
     * 判断置顶数量是否超过最大数量
     * @return 结果
     */
    private boolean toopedNum(){
        LambdaQueryWrapper<CnCommonCloud> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CnCommonCloud::getTopped, true);
        return count(queryWrapper) >= 4;
    }
}
